﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Okazuki.MVVM.PrismSupport.ViewModels;
using Okazuki.MVVM.PrismSupport.ViewModels.Utils;
using Microsoft.Practices.Prism.Commands;

namespace Okazuki.MVVM.PrismSupport.Test.ViewModels.Utils
{
    [TestClass]
    public class ViewModelReflectionUtilTest
    {
        class MockViewModel : ViewModelBase
        {
            [AutoInitCommand]
            public DelegateCommand SampleCommand { get; private set; }

            [CommandMethod]
            private void SampleExecute()
            {
            }

            [CommandMethod]
            private bool CanSampleExecute()
            {
                return true;
            }

            [AutoInitCommand]
            public DelegateCommand<string> Sample2Command { get; private set; }

            [CommandMethod]
            private void Sample2Execute(string parameter)
            {
            }

            private DelegateCommand sample3Command = new DelegateCommand(() => { });
            public DelegateCommand Sample3Command
            {
                get { return this.sample3Command; }
            }

        }

        [TestMethod]
        public void TestGetViewModelInfoSameObject()
        {
            var info = ViewModelReflectionUtil.GetViewModelInfo(typeof(MockViewModel));
            var info2 = ViewModelReflectionUtil.GetViewModelInfo(typeof(MockViewModel));

            Assert.AreSame(info, info2);
        }

        [TestMethod]
        public void TestGetViewModelInfo()
        {
            var info = ViewModelReflectionUtil.GetViewModelInfo(typeof(MockViewModel));
            Assert.AreEqual(2, info.AutoInitCommands.Count);

            // SampleCommand
            Assert.AreEqual("SampleCommand", info.AutoInitCommands["SampleCommand"].CommandName);
            Assert.AreEqual("SampleExecute", info.AutoInitCommands["SampleCommand"].ExecuteMethod.Name);
            Assert.AreEqual("CanSampleExecute", info.AutoInitCommands["SampleCommand"].CanExecuteMethod.Name);

            // Sample2Command
            Assert.AreEqual("Sample2Command", info.AutoInitCommands["Sample2Command"].CommandName);
            Assert.AreEqual("Sample2Execute", info.AutoInitCommands["Sample2Command"].ExecuteMethod.Name);
            Assert.IsNull(info.AutoInitCommands["Sample2Command"].CanExecuteMethod);

            // Commandプロパティの情報収集ができているか
            Assert.AreEqual(3, info.CommandProperties.Count);
            Assert.IsTrue(info.CommandProperties.ContainsKey("SampleCommand"));
            Assert.IsTrue(info.CommandProperties.ContainsKey("Sample2Command"));
            Assert.IsTrue(info.CommandProperties.ContainsKey("Sample3Command"));
        }
    }
}
